/*
    Document   : search.js
    Created on : Nov 19, 2012
    Author     : HongDC
*/

var xmlResult = "";

function search(str)
{
    if (str.length < 1) {
        $(".result-widget").innerHTML = "";
        return;
    }
    xmlhttp = setXmlHttpRequest();
    xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            xmlResult = parseXml(xmlhttp.responseText);
            $(".result-widget").innerHTML = makeHtml();
        }
    }
    xmlhttp.open("GET","Controller?action=Search&q="+str, true);
    xmlhttp.send();
}

function hide() {
//    $(".result-widget").innerHTML = "";
}

function makeHtml() {
    var html = "<ul>";
    var bookArray = xmlResult.getElementsByTagName("Book");
    if (bookArray.length == 0) {
        html += "<li class='no-result'>No title matched.</li>";
    }
    for (i = 0; i < bookArray.length; i++) {
        var book = bookArray[i];
        html += '<li><a href="Controller?action=BookDetails&isbn='+ book.childNodes[0].textContent +'" data-isbn="'+ book.childNodes[0].textContent +'"><div>'+
                '<img class="coverUrl" src="'+ book.childNodes[3].textContent +'">' +
                '<div class="desc">' +
                    '<div class="title">'+ book.childNodes[1].textContent +'</div>'+
                    '<div class="author">'+ book.childNodes[2].textContent +'</div>'+
                '</div>'+
            '</div></a></li>';
    }
    html += "</ul>";
    return html;
}